package com.hospital.hms.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.hospital.hms.common.CommonResult;
import com.hospital.hms.controller.form.*;
import com.hospital.hms.controller.form.SelectMedicalDeptSubListForm;
import com.hospital.hms.pojo.MedicalDeptSub;
import com.hospital.hms.service.MedicalDeptSubService;
import com.hospital.hms.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.RequestScope;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/medical/dept/sub")
@CrossOrigin(origins = "http://localhost:4000", allowCredentials = "true")
@Tag(name = "MedicalDeptSubController", description = "科室门诊管理接口")
public class MedicalDeptSubController {

    @Autowired
    MedicalDeptSubService medicalDeptSubService;

    @Operation(summary = "分页显示门诊信息接口")
    @PostMapping("/selectConditionByPage")
    @SaCheckLogin
    public CommonResult selectConditionByPage(@RequestBody @Valid SelectMedicalDeptSubByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = medicalDeptSubService.selectConditionByPage(param);
        return CommonResult.ok().put("result", pageUtils);
    }

    @Operation(summary = "添加诊室信息接口")
    @PostMapping("/insert")
    @SaCheckLogin
    public CommonResult insert(@RequestBody @Valid InsertMedicalDeptSubForm form){
        MedicalDeptSub medicalDeptSub = BeanUtil.toBean(form, MedicalDeptSub.class);
        medicalDeptSubService.insert(medicalDeptSub);
        return CommonResult.ok();
    }

    @Operation(summary = "回显诊室信息接口")
    @PostMapping("/selectById")
    @SaCheckLogin
    public CommonResult selectById(@RequestBody @Valid SelectMedicalDeptByIdForm form){
        HashMap map = medicalDeptSubService.selectById(form.getId());
        return CommonResult.ok(map);
    }

    @Operation(summary = "修改诊室信息接口")
    @PostMapping("/update")
    @SaCheckLogin
    public CommonResult update(@RequestBody @Valid UpdateMedicalDeptSubForm form){
        MedicalDeptSub medicalDeptSub = BeanUtil.toBean(form, MedicalDeptSub.class);
        medicalDeptSubService.update(medicalDeptSub);
        return CommonResult.ok();
    }

    @Operation(summary = "删除诊室信息接口")
    @PostMapping("/deleteByIds")
    @SaCheckLogin
    public CommonResult deleteByIds(@RequestBody @Valid DeleteMedicalDeptSubByIdsForm form){
        medicalDeptSubService.deleteByIds(form.getIds());
        return CommonResult.ok();
    }

    /*
    * 以下是移动端端的接口
    * */
    @PostMapping("/selectMedicalDeptSubList")
    @Operation(summary = "获取诊室列表")
    public CommonResult selectMedicalDeptSubList(@RequestBody @Valid SelectMedicalDeptSubListForm form){
        ArrayList<HashMap> list = medicalDeptSubService.selectMedicalDeptSubList(form.getDeptId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, list);
    }
}
